package com.yuehuanghun.admin.core.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yuehuanghun.admin.core.config.util.poi.ExcelUtil;
import com.yuehuanghun.admin.core.domain.SysDictData;
import com.yuehuanghun.admin.core.service.ISysDictDataService;
import com.yuehuanghun.admin.core.service.ISysDictTypeService;
import com.yuehuanghun.framework.annotation.Log;
import com.yuehuanghun.framework.annotation.Log.BusinessType;
import com.yuehuanghun.framework.domain.PageDTO;
import com.yuehuanghun.framework.web.BaseResource;
import com.yuehuanghun.mybatis.milu.data.Sort;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;

/**
 * 数据字典信息
 * 
 * @author yadmin
 */
@RestController
@RequestMapping("/system/dict/data")
@SaCheckLogin
public class SysDictDataController extends BaseResource {
	@Autowired
	private ISysDictDataService dictDataService;

	@Autowired
	private ISysDictTypeService dictTypeService;

	@SuppressWarnings("unchecked")
	@GetMapping("/list")
	@SaCheckPermission("system:dict:list")
	public PageDTO<SysDictData> list(SysDictData dictData) { //NOSONAR
		List<SysDictData> list = dictDataService.getByExampleAndSort(dictData, Sort.asc(SysDictData::getDictSort), getPage());
		return toPage(list);
	}

	@Log(title = "字典数据", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	@SaCheckPermission("system:dict:export")
	public String export(SysDictData dictData) { //NOSONAR
		List<SysDictData> list = dictDataService.getByExample(dictData);
		ExcelUtil<SysDictData> util = new ExcelUtil<>(SysDictData.class);
		return util.exportExcel(list, "字典数据");
	}

	/**
	 * 查询字典数据详细
	 */
	@GetMapping(value = "/{dictCode}")
	@SaCheckPermission("system:dict:query")
	public SysDictData getInfo(@PathVariable Long dictCode) {
		return dictDataService.getById(dictCode).orElse(null);
	}

	/**
	 * 根据字典类型查询字典数据信息
	 */
	@GetMapping(value = "/type/{dictType}")
	public List<SysDictData> dictType(@PathVariable String dictType) {
		return dictTypeService.getDictDataByType(dictType);
	}

	/**
	 * 新增字典类型
	 */
	@Log(title = "字典数据", businessType = BusinessType.INSERT)
	@PostMapping
	@SaCheckPermission("system:dict:add")
	public void add(@Validated @RequestBody SysDictData dict) { //NOSONAR
		dictDataService.insertDictData(dict);
	}

	/**
	 * 修改保存字典类型
	 */
	@Log(title = "字典数据", businessType = BusinessType.UPDATE)
	@PutMapping
	@SaCheckPermission("system:dict:edit")
	public void edit(@Validated @RequestBody SysDictData dict) { //NOSONAR
		dictDataService.updateDictData(dict);
	}

	/**
	 * 删除字典类型
	 */
	@Log(title = "字典类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dictCodes}")
	@SaCheckPermission("system:dict:remove")
	public void remove(@PathVariable Long[] dictCodes) {
		dictDataService.deleteDictDataByIds(dictCodes);
	}
}
